Page({
  data: {
    answers:[],
    babyList:[],
    isSelectTopic:true,
    curBabyIndex:0,
    totalNum:0,
    totalPersent:'50%',
    curCode:'',
    current: 1,
    skuIndex: '',
    curTopicIndex:2,
    topicData:{}
  },
  toPre(){
    console.log(23223)
  },
  onLoad() {
    //   let yunSdk = getApp().getYouZanYunSdk();
    //   yunSdk.request({
    //     isv: 'test.isv-dev.youzan.com', 
    //     path: '/demo',
    //     method: 'GET',
    //     succeed: (res) => {
    //         console.log('res', res);
    //     }, 
    //     failed: (err) => {
    //         console.log('err', err);
    //     }
    // })
    this.initData()
    wx.request({
      url: getApp().globalData.apiBaseUrl + 'question/info',
      data: {
        id: 1
      },
      method: 'get',
      // header: {
      //   "content-type": "application/x-www-form-urlencoded",
      //   "X-Access-Token": wx.getStorageSync('token'),
      //   "Authorization": wx.getStorageSync('token')
      // },
      success: function (res) {
        if (res.data.code == 200) {
        } else {
        }
      }
    })
  },
  initData(){
    
    wx.request({
      url: getApp().globalData.apiBaseUrl + 'topic/count',
      data: {
        questionId: getApp().globalData.questionId
      },
      method: 'post',
      success:  (res)=> {
        console.log(res,122)
        if (res.data.code == 200) {
          this.setData({
            totalNum:res.data.data
          })
          this.getTopic()
        }
      }
    })
    wx.request({
      url: getApp().globalData.apiBaseUrl + 'baby/findPage',
      data: {
        userId: 1
      },
      method: 'post',
      success: (res) => {
        if (res.data.code == 200) {
          this.setData({
            babyList:res.data.data.list
          })
        }
      }
    })
  },
  getTopic(){
    
    wx.request({
      url: getApp().globalData.apiBaseUrl + 'topic/info',
      data: {
        questionId: getApp().globalData.questionId,
        code:this.data.curCode
      },
      method: 'post',
      success:  (res)=> {
        console.log(res,122)
        if (res.data.code == 200) {
          this.setData({
            totalPersent:50+'%'
          })
          res.data.data.opts.some(item=>{
            item.select=false
            item.remark=''
          })
          // res.data.data.nameDesc=res.data.data.orderNo+'/'+this.data.totalNum+'.'+res.data.data.name
          res.data.data.nameDesc=res.data.data.name
          
          this.setData({
            topicData:res.data.data,
            curCode:res.data.data
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000 //持续的时间
          })
        }
      }
    })
  },
  selectBaby(e){
    this.setData({
      curBabyIndex:e.currentTarget.dataset.index
    })
  },
  selectTopic(e){
    console.log(e,12121)
    if(this.data.topicData.type==1){
      this.data.topicData.opts.some(item=>{
        item.select=false
      })
      this.setData({
        topicData:this.data.topicData
      })
    }
    let index = e.currentTarget.dataset.index
    let ifSelect = "topicData.opts[" + index + "].select";
     let selectStatus = e.currentTarget.dataset.select;
    this.setData({
      [ifSelect]: selectStatus?false: true,  //相当于取反
   })

   
   if(this.data.topicData.type==1 && this.data.topicData.opts[index].isInput!='是'){
     this.toAsk()
   }
  },
  onName(e){
    
    let index = e.currentTarget.dataset.index
    let remark = "topicData.opts[" + index + "].remark";
    this.setData({
      [remark]:e.detail.value, 
   })
   console.log(this.data.answers)
  },
  toAsk() {
    if(!this.data.isSelectTopic){
      this.setData({
        isSelectTopic:true,
      })
      return
    }
    let code=[]
    let val=[]
    let remark=[]
    this.data.topicData.opts.some(item=>{
      if(item.select){
        code.push(item.toCode)
        val.push(item.value)
        if(item.remark){
          remark.push(item.remark)
          
        }
        
      }
    })
    if(code.length<=0){
      wx.showToast({
        title: '请选择',
        icon: 'none',
        duration: 2000 //持续的时间
      })
      return
    }
    let answerItem={
      key:this.data.topicData.code,
      value:String(val),
      remark:String(remark),
    }
    this.data.answers.push(answerItem)
    this.setData({
      curCode:String(code),
      answers:this.data.answers
    })
    if(code[0]!=''){
      this.getTopic()
    }else{
      wx.showLoading({
        title: '报告生成中！'
      })
      let curBaby=this.data.babyList[this.data.curBabyIndex]
      let info={
        userId:curBaby.userId,
        userName:curBaby.userName,
        babyId:curBaby.id,
        questionId:getApp().globalData.questionId,
        answers:this.data.answers
      }
    wx.request({
      url: getApp().globalData.apiBaseUrl + 'create/report',
      data: info,
      method: 'post',
      success:  (res)=> {
        wx.hideLoading()
        console.log(res,122)
        if (res.data.code == 200) {
          
          if(res.data.data.symptom){

            wx.navigateTo({
              url: '/pages/report-result/index?id='+res.data.data.id,
            })
          }else{

            wx.navigateTo({
              url: '/pages/report-detail/index?id='+res.data.data.id,
            })
          }
          // wx.showToast({
          //   title: '答题完成！',
          //   icon: 'none',
          //   duration: 1500 //持续的时间
          // })

          // setTimeout(()=>{
          // },1500)
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000 //持续的时间
          })
        }
      }
    })
    }
  },
  bindPickerChange1: function (e) {
    this.setData({
      skuIndex: e.detail.value,
    })
  },
});